#include "head.h"

int analyze_cmd(int net_fd, MYSQL *mysql)
{
    // 登录
    request_t *request = (request_t *)malloc(sizeof(request_t));

    int exit = 0;
    while (1)
    {
        memset(request, 0, sizeof(request_t));
        //printf("等待客户端发报文\n");
        int ret = recv(net_fd, request, sizeof(request_t), 0);
        if(ret == 0)
        {
            printf("客户端断开连接\n");
            break;
        }
        printf("---请求报文---\n");
        printf("flag: %d\n", request->flag);
        printf("user_name: %s\n", request->user_name);
        printf("args: %s\n", request->args);
        printf("path: %s\n", request->path);
        printf("-------------\n");

        exit = gotodo(request, net_fd, mysql);
        if(exit == 1){
            break;
        }
    }

    free(request);
    close(net_fd);
    return 0;
}

int gotodo(request_t *request, int net_fd, MYSQL *mysql)
{
    switch (request->flag)
    {
    case LOGIN:
        // 登录函数
        login(request, net_fd, mysql);
        break;
    case CD:
        net_cd(request, net_fd, mysql);
        break;
    case LS:
        net_ls(request, net_fd, mysql);
        break;
    case PUTS:
        net_puts(request, net_fd, mysql);
        break;
    case GETS:
        net_gets(request, net_fd, mysql);
        break;
    case RM:
        net_rm(request, net_fd, mysql);
        break;
    case PWD:
        // pwd命令不需要查询数据库
        net_pwd(request, net_fd);
        break;
    case MKDIR:
        net_mkdir(request, net_fd, mysql);
        break;
    case EXIT:
        return 1;
    default:
        break;
    }
    return 0;
}

int login(request_t *request, int net_fd, MYSQL *conn)
{
    //盐值随机值种子设置
    srand(time(NULL) + pthread_self());

    //获取当前时间
    time_t timep;
    struct tm *p;
    char time1[28];
    time (&timep);
    p=gmtime(&timep);
    sprintf(time1,"%d-%d-%d %02d:%02d:%02d",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec);

    //初始化响应报文结构体
    response_t response;
    memset(&response, 0, sizeof(response_t));

    
    char *user_name = request->user_name;
    char *user_password = request->args;//参数传递密码
    MYSQL_RES *result;
    MYSQL_ROW row;

    //sql查询命令生成
    char sql_select[150];
    sprintf(sql_select, "select * from users where username = '%s'", user_name);
    printf("sql_select:%s\n", sql_select);
    //查询用户名
    if(mysql_query(conn, sql_select)){
        printf("%s \n", mysql_error(conn));
        return 0;
    }
    result = mysql_store_result(conn);
    if(result == NULL){printf("%s\n", mysql_error(conn));}

    if(!mysql_num_rows(result))
    {
        printf("注册开始\n");
        //没有查询结果
        mysql_free_result(result);

        //注册
        char salt[15];
        //printf("salt:%s\n",salt);

        char *password;
        //计算失败就重新计算
        do
        {
            bzero(salt ,sizeof(salt));
            get_salt(salt);

            password = crypt(user_password, salt);

        //printf("%s\n", password);
        }while(strlen(password) < 5);

        char sql_insert[500];
        sprintf(sql_insert, "insert into users values (NULL, '%s', '%s', '%s', '%s')", user_name, salt, password, time1);
        mysql_query(conn, sql_insert);

        //获取新用户的uid
        MYSQL_RES *uid_res;
        MYSQL_ROW uid_row;
        char sql_get_uid[200];
        sprintf(sql_get_uid, "select id from users where username = '%s'", user_name);
        mysql_query(conn, sql_get_uid);
        uid_res = mysql_store_result(conn);
        uid_row = mysql_fetch_row(uid_res);
        int uid = atoi(uid_row[0]);
        //printf("get_uid\n");
        //在files中添加用户文件夹
        char sql_insert_file[255];
        //id, filename, user_id, father_id, path, type, file_hash, create_time, file_complete, file_size, delete_flag
        sprintf(sql_insert_file, "insert into files values (NULL, '%s', %d, -1, '/', 0, NULL, '%s', 1, 0, 1) ", user_name, uid, time1);
        mysql_query(conn, sql_insert_file);

        response.status = 0;//response.status = 0 表示成功, 1 表示失败
        strcpy(response.args, "用户注册成功");
        mysql_free_result(uid_res);
    }
    else
    {
        printf("登录开始\n");
        row = mysql_fetch_row(result);
        char *salt = row[2];
        char *password = row[3];
        char *encrypted = crypt(user_password, salt);
        if(strcmp(password, encrypted) == 0)
        {
            //登录成功
            response.status = 0;
            strcpy(response.args,"登录成功");
        }
        else
        {
            //密码错误，登录失败
            response.status = 1;
            strcpy(response.args, "密码错误");
        }
        mysql_free_result(result);
    }
    printf("end ifelse\n");
    send(net_fd, &response, sizeof(response_t), 0);
    return 0;
    mysql_free_result(result);
}

//随机生成盐值
char *get_salt(char *salt)
{
//    srand(time(NULL));
    char sample[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#%^&*+/";
    int len = sizeof(sample);
    strcpy(salt, "$6$");
    
    for(int i = 3; i < 11;i ++)
    {
        salt[i] = sample[rand() % (len - 1)];
    }
    strcat(salt, "$");
    return salt;
}

